package com.zenithmind.library.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.library.service.AuthorStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 作者统计管理控制器
 * 遵循单一职责原则：只负责作者统计相关的HTTP请求处理
 * 遵循依赖倒置原则：依赖抽象接口而非具体实现
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Slf4j
@RestController
@RequestMapping("/api/library/author-statistics")
@RequiredArgsConstructor
@Tag(name = "作者统计管理", description = "作者统计数据的查询、更新、分析等功能")
public class AuthorStatisticsController {

    private final AuthorStatisticsService authorStatisticsService;

    @PostMapping("/{authorId}/book-count/update")
    @Operation(summary = "更新作者图书数量", description = "更新指定作者的图书数量统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> updateAuthorBookCount(
            @Parameter(description = "作者ID", required = true)
            @PathVariable String authorId) {
        log.info("更新作者图书数量，作者ID：{}", authorId);
        return authorStatisticsService.updateAuthorBookCount(authorId);
    }

    @PostMapping("/{authorId}/borrow-count/update")
    @Operation(summary = "更新作者总借阅次数", description = "更新指定作者的总借阅次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> updateAuthorTotalBorrowCount(
            @Parameter(description = "作者ID", required = true)
            @PathVariable String authorId) {
        log.info("更新作者总借阅次数，作者ID：{}", authorId);
        return authorStatisticsService.updateAuthorTotalBorrowCount(authorId);
    }

    @PostMapping("/{authorId}/book-count/increment")
    @Operation(summary = "增加作者图书数量", description = "增加作者的图书数量统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> incrementAuthorBookCount(
            @Parameter(description = "作者ID", required = true)
            @PathVariable String authorId) {
        log.info("增加作者图书数量，作者ID：{}", authorId);
        return authorStatisticsService.incrementAuthorBookCount(authorId);
    }

    @PostMapping("/{authorId}/book-count/decrement")
    @Operation(summary = "减少作者图书数量", description = "减少作者的图书数量统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> decrementAuthorBookCount(
            @Parameter(description = "作者ID", required = true)
            @PathVariable String authorId) {
        log.info("减少作者图书数量，作者ID：{}", authorId);
        return authorStatisticsService.decrementAuthorBookCount(authorId);
    }

    @PostMapping("/{authorId}/borrow-count/increment")
    @Operation(summary = "增加作者总借阅次数", description = "增加作者的总借阅次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> incrementAuthorTotalBorrowCount(
            @Parameter(description = "作者ID", required = true)
            @PathVariable String authorId) {
        log.info("增加作者总借阅次数，作者ID：{}", authorId);
        return authorStatisticsService.incrementAuthorTotalBorrowCount(authorId);
    }
}
